package com.kunya.product.service.impl.product;

import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.common.util.DateTimeUtil;
import com.kunya.common.util.FTPUtil;
import com.kunya.product.dao.product.ProductImageMapper;
import com.kunya.common.util.MD5Util;
import com.kunya.product.po.ProductImage;
import com.kunya.product.service.product.IProductImageService;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zp
 * @Description: 商品图片接口实现类
 * @date 2018/6/13 15:53
 */
@Service("iProductImageService")
public class ProductImageServiceImpl implements IProductImageService{

    @Autowired
    private ProductImageMapper productImageMapper;

    /**
     * @author zp
     * @Description:根据商品id查询商品图片集合
     * @param
     * @date 2018/6/13 17:12
     */
    public ServerResponse< List<ProductImage>> getProductImagesByProductId(Long productId,String tablePrefix){
        if(productId==null|| StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        List<ProductImage> productImageList=productImageMapper.selectProductImagesByProductId(productId,tablePrefix);
        if (CollectionUtils.isEmpty(productImageList)){
            return ServerResponse.createByErrorMessage("商品图片不存在");
        }
        return  ServerResponse.createBySuccess(productImageList);
    }

    @Override
    public ServerResponse deleteProductImageByProductId(Long productId, String tablePrefix) throws IOException {
        if(productId==null||StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        ServerResponse<List<ProductImage>> response = getProductImagesByProductId(productId, tablePrefix);
        if(!response.isSuccess()){ return response; }
        for (ProductImage tmp : response.getData()) {
            FTPUtil.del(tmp.getPicImg());
        }
        return productImageMapper.deleteProductImageByProductId(productId,tablePrefix)>0?
                ServerResponse.createBySuccess("删除商品图片成功"):ServerResponse.createByErrorMessage("删除商品图片失败");
    }

    @Override
    public ServerResponse deleteProdudtImageById(Long picImgId,String tablePrefix) throws IOException {
        if(picImgId==null|| StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        ProductImage tmp = productImageMapper.selectByPrimaryKey(picImgId, tablePrefix);
        if(tmp==null){
            return ServerResponse.createByErrorMessage("商品图片ID不存在!");
        }
        FTPUtil.del(tmp.getPicImg());
        return productImageMapper.deleteByPrimaryKey(picImgId, tablePrefix)==1?
                ServerResponse.createBySuccess("删除商品图片成功"):ServerResponse.createByErrorMessage("删除商品图片失败");
    }

    @Override
    public ServerResponse uploadProductImage(MultipartFile file,Long productId,Integer sort,String tablePrefix) throws IOException {
        if(file==null|| sort==null|| productId==null || StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        //TODO 从Shrio当中获取当前登录的用户名
        String username="Suimg";
        String newFileName=UUID.randomUUID().toString()+"."+FilenameUtils.getExtension(file.getOriginalFilename()).toLowerCase();
        String urlPath = String.format("/%s/%s/",MD5Util.MD5EncodeUtf8(username),MD5Util.MD5EncodeUtf8(DateTimeUtil.getCurrentDate()));
        FTPUtil.upload(file.getInputStream(),urlPath,newFileName);
        ProductImage productImage=new ProductImage();
        if(sort!=null){
            productImage.setSort((byte)sort.intValue());
        }
        productImage.setProductId(productId);
        productImage.setPicImg(urlPath+newFileName);
        productImage.setCreateTime(new Date());
        productImage.setCreateBy(username);
        productImage.setStatus((byte)1);
        productImageMapper.insert(productImage,tablePrefix);
        return ServerResponse.createBySuccess("上传商品图片成功");
    }

    @Override
    public ServerResponse displayProductImage(Long picImgId, String tablePrefix) {
        if(picImgId==null|| StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        ProductImage productImage = productImageMapper.selectByPrimaryKey(picImgId, tablePrefix);
        if(productImage==null){
            return ServerResponse.createByErrorMessage("商品图片ID不存在!");
        }
        productImage.setStatus((byte)(productImage.getStatus().equals((byte)1)?0:1));
        return productImageMapper.updateByPrimaryKey(productImage,tablePrefix)==1?
                ServerResponse.createBySuccess("显示/隐藏商品图片成功"):ServerResponse.createByErrorMessage("显示/隐藏商品图片失败!");
    }

}
